import 'package:flutter/material.dart';

@immutable
class CDColorTheme extends ThemeExtension<CDColorTheme> {
  const CDColorTheme({
    required this.disabledButtonBack,
    required this.headLargeTitle,
    required this.textColor3,
    required this.textColor2,
    required this.textColor1,
    required this.brand1,
    required this.gy4,
    required this.bgColorF7,
    required this.warning5,
    required this.gray6,
    required this.gray1,
    required this.gray2,
  });

  final Color? disabledButtonBack; // 禁用按钮
  final Color? headLargeTitle; // 大标题颜色
  final Color? textColor3;
  final Color? textColor2;
  final Color? textColor1;
  final Color? brand1;
  final Color? gy4;
  final Color? bgColorF7; // F7背景色
  final Color? warning5;
  final Color? gray6;
  final Color? gray1;
  final Color? gray2;

  @override
  CDColorTheme copyWith({
    Color? disabledButtonBack,
    Color? headLargeTitle,
    Color? textColor3,
    Color? textColor2,
    Color? textColor1,
    Color? brand1,
    Color? gy4,
    Color? bgColorF7,
    Color? warning5,
    Color? gray6,
    Color? gray1,
    Color? gray2,
  }) {
    return CDColorTheme(
      disabledButtonBack: disabledButtonBack ?? this.disabledButtonBack,
      headLargeTitle: headLargeTitle ?? this.headLargeTitle,
      textColor3: textColor3 ?? this.textColor3,
      textColor2: textColor2 ?? this.textColor2,
      textColor1: textColor1 ?? this.textColor1,
      brand1: brand1 ?? this.brand1,
      gy4: gy4 ?? this.gy4,
      bgColorF7: bgColorF7 ?? this.bgColorF7,
      warning5: warning5 ?? this.warning5,
      gray1: gray1 ?? this.gray1,
      gray6: gray6 ?? this.gray6,
      gray2: gray2 ?? this.gray2,
    );
  }

  @override
  CDColorTheme lerp(ThemeExtension<CDColorTheme>? other, double t) {
    // 检查 other 是否是 CustomColors 类型，如果不是，则无法进行插值，直接返回当前实例。
    if (other is! CDColorTheme) {
      return this;
    }

    // 使用 Color.lerp 对每个颜色属性进行线性插值。
    // Color.lerp(a, b, t) 会根据 t 的值返回一个介于颜色 a 和颜色 b 之间的颜色。
    return CDColorTheme(
      disabledButtonBack: Color.lerp(
        disabledButtonBack,
        other.disabledButtonBack,
        t,
      ),
      headLargeTitle: Color.lerp(headLargeTitle, other.headLargeTitle, t),
      textColor3: Color.lerp(textColor3, other.textColor3, t),
      textColor2: Color.lerp(textColor2, other.textColor2, t),
      textColor1: Color.lerp(textColor1, other.textColor1, t),
      brand1: Color.lerp(brand1, other.brand1, t),
      gy4: Color.lerp(gy4, other.gy4, t),
      bgColorF7: Color.lerp(bgColorF7, other.bgColorF7, t),
      warning5: Color.lerp(warning5, other.warning5, t),
      gray6: Color.lerp(gray6, other.gray6, t),
      gray1: Color.lerp(gray1, other.gray1, t),
      gray2: Color.lerp(gray2, other.gray2, t),
    );
  }

  // (可选但推荐) 为了方便调试，可以重写 toString 方法
  @override
  String toString() =>
      'CustomColors(disabledButtonBack: $disabledButtonBack, headLargeTitle: $headLargeTitle)';

  // (可选但推荐) 为了让 ThemeExtension 能够被正确比较，可以重写 == 和 hashCode
  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;
    return other is CDColorTheme &&
        other.disabledButtonBack == disabledButtonBack &&
        other.headLargeTitle == headLargeTitle &&
        other.textColor3 == textColor3 &&
        other.textColor2 == textColor2 &&
        other.textColor1 == textColor1 &&
        other.brand1 == brand1 &&
        other.gy4 == gy4 &&
        other.bgColorF7 == bgColorF7 &&
        other.warning5 == warning5 &&
        other.gray1 == gray1 &&
        other.gray2 == gray2;
  }

  @override
  int get hashCode =>
      disabledButtonBack.hashCode ^
      headLargeTitle.hashCode ^
      textColor3.hashCode ^
      textColor2.hashCode ^
      textColor1.hashCode ^
      textColor1.hashCode ^
      gy4.hashCode ^
      bgColorF7.hashCode ^
      warning5.hashCode ^
      gray1.hashCode ^
      gray2.hashCode;
}
